package com.bsg.upm.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.MonitorEventService;
/**
 * 
 * @author yucs
 *
 */
@Controller
@RequestMapping("/v1.0/monitor/event")
public class MonitorEventController {
	@Resource
	MonitorEventService monitorEventService;

	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public RespJson list(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
	 		
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			Map<String, Object> searchParamMap = new HashMap<String, Object>();

			searchParamMap.put("siteId", paramMap.get("site"));
			
			if (paramMap.containsKey("start_time")) {
				String start_time = (String) paramMap.get("start_time");
				searchParamMap.put("start", format.format(new Long(start_time)));
			}
			
			if (paramMap.containsKey("end_time")) {
				String end_time = (String) paramMap.get("end_time");
				searchParamMap.put("end", format.format(new Long(end_time)));
			}
			

			if (paramMap.containsKey("type")) {
				searchParamMap.put("type", paramMap.get("type"));
			}
			
			if (paramMap.containsKey("host")) {
				searchParamMap.put("hostId", paramMap.get("host"));
			}
			
			if (paramMap.containsKey("owner")) {
				searchParamMap.put("owner", paramMap.get("owner"));
			}
			

			respJson = monitorEventService.getShowEvent(searchParamMap);
			if (respJson.getStatus() != HttpStatus.SC_OK) {
				response.setStatus(respJson.getStatus());
			}
		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

}
